package com.guducat.collegeWeb.dto.response;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 用户数据同步结果DTO
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "用户数据同步结果")
public class UserDataSyncResultDTO {

    @Schema(description = "同步是否成功", example = "true")
    private Boolean success;

    @Schema(description = "同步类型", example = "upload")
    private String syncType;

    @Schema(description = "同步时间", example = "2024-01-15T10:30:00")
    private LocalDateTime syncTime;

    @Schema(description = "总处理数量", example = "100")
    private Integer totalCount;

    @Schema(description = "成功数量", example = "95")
    private Integer successCount;

    @Schema(description = "冲突数量", example = "3")
    private Integer conflictCount;

    @Schema(description = "失败数量", example = "2")
    private Integer failedCount;

    @Schema(description = "错误信息")
    private String errorMessage;

    @Schema(description = "同步详情")
    private List<DataTypeSyncDetail> syncDetails;

    @Schema(description = "下载时返回的用户数据")
    private List<UserDataItemDTO> downloadData;

    @Schema(description = "冲突数据列表")
    private List<ConflictDataDTO> conflicts;

    /**
     * 数据类型同步详情
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(description = "数据类型同步详情")
    public static class DataTypeSyncDetail {

        @Schema(description = "数据类型", example = "favorite")
        private String dataType;

        @Schema(description = "处理数量", example = "50")
        private Integer processedCount;

        @Schema(description = "成功数量", example = "48")
        private Integer successCount;

        @Schema(description = "冲突数量", example = "1")
        private Integer conflictCount;

        @Schema(description = "失败数量", example = "1")
        private Integer failedCount;

        @Schema(description = "错误信息")
        private List<String> errors;
    }

    /**
     * 用户数据项DTO（用于下载响应）
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(description = "用户数据项")
    public static class UserDataItemDTO {

        @Schema(description = "数据类型", example = "favorite")
        private String dataType;

        @Schema(description = "文章ID", example = "1001")
        private Integer newsId;

        @Schema(description = "文章标题", example = "校园新闻标题")
        private String newsTitle;

        @Schema(description = "文章URL", example = "/news/1001")
        private String newsUrl;

        @Schema(description = "文章分类", example = "校园新闻")
        private String newsCategory;

        @Schema(description = "文章所属行政级别", example = "学院级")
        private String newsSection;

        @Schema(description = "扩展数据")
        private Map<String, Object> extraData;

        @Schema(description = "创建时间", example = "2024-01-15T10:30:00")
        private LocalDateTime createTime;

        @Schema(description = "更新时间", example = "2024-01-15T10:30:00")
        private LocalDateTime updateTime;
    }

    /**
     * 冲突数据DTO
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(description = "冲突数据")
    public static class ConflictDataDTO {

        @Schema(description = "数据类型", example = "favorite")
        private String dataType;

        @Schema(description = "文章ID", example = "1001")
        private Integer newsId;

        @Schema(description = "冲突原因", example = "数据已存在且时间戳不同")
        private String conflictReason;

        @Schema(description = "服务器端数据")
        private UserDataItemDTO serverData;

        @Schema(description = "客户端数据")
        private UserDataItemDTO clientData;

        @Schema(description = "建议的解决方案", example = "keep_latest")
        private String suggestedResolution;
    }

    /**
     * 创建成功结果
     */
    public static UserDataSyncResultDTO success(String syncType, Integer totalCount, Integer successCount) {
        UserDataSyncResultDTO result = new UserDataSyncResultDTO();
        result.setSuccess(true);
        result.setSyncType(syncType);
        result.setSyncTime(LocalDateTime.now());
        result.setTotalCount(totalCount);
        result.setSuccessCount(successCount);
        result.setConflictCount(0);
        result.setFailedCount(totalCount - successCount);
        return result;
    }

    /**
     * 创建失败结果
     */
    public static UserDataSyncResultDTO failure(String syncType, String errorMessage) {
        UserDataSyncResultDTO result = new UserDataSyncResultDTO();
        result.setSuccess(false);
        result.setSyncType(syncType);
        result.setSyncTime(LocalDateTime.now());
        result.setErrorMessage(errorMessage);
        result.setTotalCount(0);
        result.setSuccessCount(0);
        result.setConflictCount(0);
        result.setFailedCount(0);
        return result;
    }
}
